package com.cloud.translation.Dao;

import android.content.Context;
import android.util.Log;

import com.cloud.translation.Model.Model;

import java.util.List;

public class DaoUtils {
    private static final String TAG = DaoManager.class.getSimpleName();
    private DaoManager mManager;

    public DaoUtils(Context context){
        mManager = DaoManager.getInstance();
        mManager.init(context);
    }

    public boolean insert(Model dao){
        boolean flag = false;
        flag = mManager.getDaoSession().getModelDao().insert(dao) == -1 ? false : true;
        Log.i(TAG, "insert Meizi :" + flag + "-->" + dao.toString());
        return flag;
    }


    public boolean insert(final List<Model> meiziList) {
        boolean flag = false;
        try {
            mManager.getDaoSession().runInTx(new Runnable() {
                @Override
                public void run() {
                    for (Model dao : meiziList) {
                        mManager.getDaoSession().insertOrReplace(dao);
                    }
                }
            });
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }


    public boolean update(Model dao){
        boolean flag = false;
        try {
            mManager.getDaoSession().update(dao);
            flag = true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag;
    }

    public boolean delete(Model dao){
        boolean flag = false;
        try {
            mManager.getDaoSession().delete(dao);
            flag = true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag;
    }


    public boolean deleteAll(){
        boolean flag = false;
        try {
            //按照id删除
            mManager.getDaoSession().deleteAll(Model.class);
            flag = true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag;
    }


    public List<Model> queryAll(){
        return mManager.getDaoSession().loadAll(Model.class);
    }

    public Model queryById(long key){
        return mManager.getDaoSession().load(Model.class, key);
    }


    public List<Model> queryByNativeSql(String sql, String[] conditions){
        return mManager.getDaoSession().queryRaw(Model.class, sql, conditions);
    }
}
